package ExerciseClass;

import java.util.Random;

public class ExerciseImpl<T> implements ExerciseInterface<T>{

    private T [] arr;
    private int count=0;
    public ExerciseImpl(T[] arr) {
        this.arr = arr;
    }

    @Override
    public boolean isEmpty() {
        for (int i =0;i<arr.length;i++){
            if (arr[i]!=null){
                return true;
            }
        }
        return false;
    }

    @Override
    public int size() {
        //记录有效长度
        return count;
    }

    @Override
    public T search(T key) {
        for (int i =0 ; i<arr.length;i++){
            if(key.equals(arr[i])){
                return arr[i];
            }
        }
    return null;
    }

    @Override
    public boolean contains(T key) {
        for (int i =0;i<arr.length;i++){
            if (key.equals(arr[i])){
                System.out.println("存在这个数为:"+arr[i]);
                return true;
            }
        }
        System.out.println("不存在这个数");
        return false;
    }


    @Override
    public boolean add(T x) {

        Random random = new Random();
        int i=random.nextInt(arr.length);
        boolean b = true;
        for (int j =0; j<arr.length;j++){
            if(arr[j]==null){
                b=false;
                break;
            }
        }
        if(b){
            //创建临时数组
            T [] arr1 =arr;
            //扩容
            arr = (T[]) new Object[arr1.length*2];
            for (int j = 0;j<arr1.length;j++){
                    arr[j]=arr1[j];
            }
        }

        while(true){
            if(arr[i]==null){
                arr[i]=  x;
                count++;
//                System.out.println(arr[i]);
                break;
            }else {
                i=random.nextInt(arr.length);
            }
        }


        return true;
    }

    @Override
    public T remove(T key) {
        int num=count;
          for (int i = 0;i<arr.length;i++){
                if(key.equals(arr[i])){
                    arr[i]=null;
                    count--;
                    System.out.println("删除成功");
                }
          }
          if(count!=num){
              return key;
          }
          return null;
    }

    @Override
    public void clear() {
        //被删除的个数  防止如果恰巧 3个数在前面 就可以不用遍历整个数组长度
        int num=0;
        for (int i = 0; i < arr.length && num<count; i++) {
            if(arr[i]==null){
                continue;
            }else {
                arr[i]=null;
                num++;
            }
        }
        count=0;
    }

    @Override
    public String toString() {
//        防止如果恰巧 3个数在前面 就可以不用遍历整个数组长度
        int num=0;
        for(int i = 0; i<arr.length&&num<count;i++){
            if (null==arr[i]){
                continue;
            }else {
                num++;
                System.out.println(arr[i]);
            }
        }
        if(num==0){
            System.out.println("当前数组为空");
        }else{
            System.out.println("遍历成功");
        }
        return "遍历成功";
    }

}
